package carpediem.xin.tamplate.propertie;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;

import carpediem.xin.tamplate.application.CustomApplication;

/**
 * Created by zhaijianwei on 16/12/8.
 */

public class CacheProp {
    /*目录路径*/
    private static final String ROOT_DIR = "CloudFootball";
    private static final String DOWNLOAD_CACHE_DIR = "Download";//download file cache dir
    private static final String HTTP_CACHE_DIR = "OkHttpCache";//http response cache dir
    private static final String IMAGE_CACHE_DIR = "ImageCache";//image cache dir
    private static final String DEBUG_DIR = "Debug";//download file cache dir
    /*缓存空间最低要求*/
    private static final int MIN_CACHE_REQUIRE_MB = 10;
    public static final long MIN_CACHE_REQUIRE_BYTES = MIN_CACHE_REQUIRE_MB * 1024L * 1024L;

    /**
     * 获取 debug用文件的外部存储目录地址文件
     */
    public static File getDebugDir() {
        return getCacheDir(DEBUG_DIR);
    }

    /**
     * 获取 缓存根目录地址文件
     */
    public static File getDownloadCacheDir() {
        return getCacheDir(DOWNLOAD_CACHE_DIR);
    }

    /**
     * 获取 http请求缓存目录地址文件
     */
    public static File getOkHttpCacheDir() {
        return getCacheDir(HTTP_CACHE_DIR);
    }

    /**
     * 获取 图片缓存根目录地址文件
     */
    public static File getImageCacheDir() {
        return getCacheDir(IMAGE_CACHE_DIR);
    }

    /**
     * 根据末级目录路径创建 缓存目录文件
     *
     * @param cacheDirPath 末级目录路径
     */
    private static File getCacheDir(String cacheDirPath) {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) && getAvailableExternalMemorySize() > MIN_CACHE_REQUIRE_BYTES) {
            String cachePath = new StringBuilder(Environment.getExternalStorageDirectory().getPath()).append(File.separator)
                    .append(ROOT_DIR).append(File.separator)
                    .append(cacheDirPath).toString();
            File cacheDir = new File(cachePath);
            if (!cacheDir.exists()) {
                cacheDir.mkdirs();
            }
            if (cacheDir.exists()) {
                return cacheDir;
            } else {
                return null;
            }
        } else if (getAvailableInternalMemory() > MIN_CACHE_REQUIRE_MB) {
            return CustomApplication.getContext().getDir(HTTP_CACHE_DIR, Context.MODE_PRIVATE);
        } else {
            return null;
        }
    }

    /**
     * 获取外部存储空间大小
     */
    private static long getAvailableExternalMemorySize() {
        long availableExternalMemorySize = 0;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            availableExternalMemorySize = availableBlocks * blockSize;
        } else if (Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {
            availableExternalMemorySize = 0;
        }

        return availableExternalMemorySize;
    }

    /**
     * 获取可用手机存储空间大小
     *
     * @return 单位：M
     */
    public static long getAvailableInternalMemory() {
        File file = Environment.getDataDirectory();
        //get memory with file
        String path = file.getPath();
        StatFs sf = new StatFs(path);
        long blockSize = sf.getBlockSize();
        long availCount = sf.getAvailableBlocks();
        long availSize = availCount * blockSize;
        return availSize / 1024 / 1024;
    }

}
